package com.itdianzi.controller.opinion;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.opinion.Opinion;
import com.itdianzi.domain.opinion.OpinionExample;
import com.itdianzi.domain.system.User;
import com.itdianzi.service.OpinionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/opinion")
public class OpinionController extends BaseController {

    @Reference
    private OpinionService opinionService;

    /*跳转 用户反馈或系统反馈 页面*/
    @RequestMapping("/idea")
    public String idea() {

        User user = (User) session.getAttribute("loginUser");

        String userName = user.getUserName();

        request.setAttribute("userName", userName);

        request.setAttribute("companyId", companyId);

        request.setAttribute("companyName", companyName);

        Integer degree = user.getDegree();

        /*系统反馈页面*/
        if (degree == 1) {

            return "opinion/systemIdea";

            /*用户反馈页面*/
        } else if (degree > 1) {
            return "opinion/userIdea";
        }

       return null;
    }

    /*保存 用户反馈或系统反馈*/
    @RequestMapping("/ideaSubmit")
    public String ideaSubmit(String userName,String idea) {


        User user = (User) session.getAttribute("loginUser");
        Integer degree = user.getDegree();

        /*保存系统反馈*/
        if (degree == 1) {

            Opinion opinion = new Opinion();
            opinion.setUserName(userName);
            opinion.setCompanyId(companyId);
            opinion.setCompanyName(companyName);
            opinion.setIdea(idea);

            //1用户/0系统 反馈
            opinion.setIdeaType(0);

            //反馈内容状态 1未读 0已读
            opinion.setStateS(1);

            //提出时间
            opinion.setInputTime(new Date());

            //联系电话
            opinion.setTel(88888);

            opinionService.save(opinion);
            return "redirect:/opinion/idea.do";

            /*保存用户反馈*/
        } else if (degree > 1) {

            Opinion opinion = new Opinion();
            opinion.setUserName(userName);
            opinion.setCompanyId(companyId);
            opinion.setCompanyName(companyName);
            opinion.setIdea(idea);

            //1用户/0系统 反馈
            opinion.setIdeaType(1);

            //用户状态 1未读 0已读
            opinion.setStateU(1);

            //提出时间
            opinion.setInputTime(new Date());

            //联系电话
            opinion.setTel(99999);

            opinionService.save(opinion);
            return "redirect:/opinion/idea.do";
        }

        return null;

    }



    /*将查看过得 用户反馈或系统反馈 的状态改为已读0*/
    @RequestMapping("/messageChecked")
    public String messageChecked(String id) {

        System.out.println("id-------->" + id);
        User user = (User) session.getAttribute("loginUser");
        Integer degree = user.getDegree();

        /*只有SaaS可以观看系统反馈*/
        if (degree == 0) {

            Opinion opinion = opinionService.findOpinionById(id);
            opinion.setStateS(0);
            request.setAttribute("opinion", opinion);
            opinionService.update(opinion);

            /*只有企业可以观看用户反馈*/
        } else if (degree == 1) {

            Opinion opinion = opinionService.findOpinionById(id);
            opinion.setStateU(0);
            request.setAttribute("opinion", opinion);
            opinionService.update(opinion);
        }


        return "opinion/message";
    }



    /*返回登录页面*/
    @RequestMapping("/toHome")
    public String toHome() {
        return "home/home";
    }



    /*从铃铛图标中 查看用户反馈或系统反馈 所有未读1反馈信息*/
    @RequestMapping("/unread")
    @ResponseBody
    public List<Opinion> unread() {

        User user = (User) session.getAttribute("loginUser");
        Integer degree = user.getDegree();

        OpinionExample opinionExample = new OpinionExample();
        List<Opinion> unread = null;

        /*只有SaaS可以观看系统反馈*/
        if (degree == 0) {

            OpinionExample.Criteria criteria = opinionExample.createCriteria();
            //1用户/0系统 反馈
            criteria.andIdeaTypeEqualTo(0);
            //未读1
            criteria.andStateSEqualTo(1);
            unread = opinionService.unread(opinionExample);


            /*只有企业可以观看用户反馈*/
        } else if (degree == 1) {

            OpinionExample.Criteria criteria = opinionExample.createCriteria();
            //1用户/0系统 反馈
            criteria.andIdeaTypeEqualTo(1);
            //未读1
            criteria.andStateUEqualTo(1);
            unread = opinionService.unread(opinionExample);

        }


        return unread;
    }


    /*查看用户反馈或系统反馈所有信息并在查看后状态改为已读0*/
    @RequestMapping("/checkHistory")
    public String checkHistory() {
        /*重新设置时间字段*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");

        User user = (User) session.getAttribute("loginUser");
        Integer degree = user.getDegree();

        OpinionExample opinionExample = new OpinionExample();

        /*只有SaaS可以观看系统反馈*/
        if (degree == 0) {

            OpinionExample.Criteria criteria = opinionExample.createCriteria();

            //1用户/0系统 反馈
            criteria.andIdeaTypeEqualTo(0);
            List<Opinion> list = opinionService.findAll(opinionExample);

            for (Opinion opinion : list) {
                /*存入时间字段*/
                opinion.setTime(simpleDateFormat.format(opinion.getInputTime()));

                /*改为已读*/
                opinion.setStateS(0);

                opinionService.update(opinion);
            }

            request.setAttribute("user", user);
            request.setAttribute("list", list);
            return "opinion/allMessage";

        }

        /*只有企业可以观看用户反馈*/
        if (degree == 1) {


            OpinionExample.Criteria criteria = opinionExample.createCriteria();

            //1用户/0系统 反馈
            criteria.andIdeaTypeEqualTo(1);
            criteria.andCompanyIdEqualTo(companyId);
            List<Opinion> list = opinionService.findAll(opinionExample);

            for (Opinion opinion : list) {

                /*存入时间字段*/
                opinion.setTime(simpleDateFormat.format(opinion.getInputTime()));

                /*改为已读*/
                opinion.setStateU(0);
                opinionService.update(opinion);
            }
            request.setAttribute("user", user);
            request.setAttribute("list", list);
            return "opinion/allMessage";
        }

        return null;
    }

    @RequestMapping("/deleteIdea")
    public String deleteIdea(String id) {
        opinionService.delete(id);
        return "redirect:/opinion/checkHistory.do";
    }

}
